<template>
	<view class="uni-padding-wrap uni-common-mt">
		<view class="uni-btn-v">
			<!-- #ifdef MP-WEIXIN -->
			<view v-if="hasLogin">
				<view v-if="showBind">
					<view class="wxTipTitle">未检测到企业绑定信息</view>
					<view class="wxTipCon">绑定企业信息后无需在健康打卡页面手动输入企业信息。</view>
					<navigator url="../employee/bind/bind" hover-class="navigator-hover">
						<button type="primary">绑定企业信息</button>
					</navigator>
				</view>
				<navigator url="../form/form" hover-class="navigator-hover">
					<button type="default">{{btnTxt}}</button>
				</navigator>
				<button v-if="btnTxt === '员工健康打卡'" @click="resetEmployee" type="primary">解除绑定企业信息</button>
			</view>
			<view v-else>
				<view class="wxTipTitle">未检测到登录信息</view>
				<view class="wxTipCon">登录后可获取绑定的企业信息，在健康打卡页面无需再手动输入。如未绑定，可登录后进行绑定操作。</view>
				<button open-type="getUserInfo" lang="zh_CN" @getuserinfo="onGotUserInfo" class="weui-btn btn_indo" type="primary">微信登录</button>
				<navigator url="../form/form" hover-class="navigator-hover">
					<button type="default">{{btnTxt}}</button>
				</navigator>
			</view>
			<!-- #endif -->

			<!-- #ifndef MP-WEIXIN -->
			<navigator url="../form/form" hover-class="navigator-hover">
				<button type="default">员工健康打卡</button>
			</navigator>
			<navigator url="../manager/index" hover-class="navigator-hover">
				<button type="default">管理入口</button>
			</navigator>
			<!-- #endif -->
		</view>
		<view class="action-row">
			<navigator url="../cooperate/cooperate">商业合作</navigator>
			<text> | </text>
			<navigator url="../suggest/suggest">意见反馈</navigator>
		</view>
		<hvl-contact title="客服联系人" />
	</view>
</template>

<script>
	import hvlContact from '../../components/hvl-contact.vue'
	import { shareConfig } from '../../common/hvl-const.js'
	const app = getApp();
	export default {
		components: {
			hvlContact
		},
		data() {
			return {
				hasLogin: false,
				showBind: false,
				btnTxt: '直接打卡'
			}
		},
		// #ifdef  MP-WEIXIN
		onShareAppMessage(res) {
			return {
				title: shareConfig.title,
				path: shareConfig.path
			}
		},
		// #endif
		methods: {
			checkTokenCache: function() {
				console.log('检查本地缓存', app.globalData);
				app.globalData.token = uni.getStorageSync('token');
				if (!app.globalData.token) {
					console.log('本地缓存安全令牌无效')
					return false
				}
				console.log('本地缓存正常', app.globalData);
				return true;
			},
			getEmployee: function() {
				uniCloud.callFunction({
					name: 'company_wx_auth',
					data: {
						reqType: 2,
						token: app.globalData.token
					}
				}).then((res) => {
					uni.hideLoading()
					console.log('员工信息:', res);
					const employee = res.result || {};
					app.globalData.employee = employee;
					this.hasLogin = true;
					if (employee.company) {
						this.showBind = false;
						this.btnTxt = '员工健康打卡';
					} else {
						uni.showToast({
							title: '未绑定企业信息',
							icon: 'none'
						});
						this.showBind = true;
						this.btnTxt = '直接打卡';
					}
				}).catch((err) => {
					uni.hideLoading();
					uni.showToast({
						title: `获取员工信息失败：${err.message}`,
						icon: 'none'
					});
				})
			},
			resetEmployee() { // 解除绑定企业信息
				if (!app.globalData.token) {
					return;
				}
				uni.showLoading({
					mask: true,
					title: '处理中...'
				});
				uniCloud.callFunction({
					name: 'company_wx_auth',
					data: {
						reqType: 4,
						token: app.globalData.token
					}
				}).then((res) => {
					uni.hideLoading()
					if (res.result.status) {
						app.globalData.employee = res.result.employee;
						uni.showToast({
							title: '解绑成功',
							icon: 'success'
						});
						this.showBind = true;
						this.btnTxt = '直接打卡';
					} else {
						uni.showToast({
							title: `解绑失败：${res.result.msg}`,
							icon: 'none'
						});
					}
				}).catch((err) => {
					uni.hideLoading()
					uni.showToast({
						title: `解绑失败：${err.message}`,
						icon: 'none'
					});
				})
			},
			login() {
				uni.showLoading({
					mask: true,
					title: '登录中...'
				});
				// 1.1 获取微信 授权信息 & 用户信息
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						app.globalData.code = loginRes.code;
						// 1.2 获取安全令牌
						uniCloud.callFunction({
							name: 'company_wx_auth',
							data: {
								reqType: 1,
								code: loginRes.code,
								userInfo: app.globalData.userInfo
							}
						}).then((res) => {
							if (res.result) {
								app.globalData.token = res.result;
								uni.setStorageSync('token', res.result);
								// 1.3 获取绑定员工信息
								this.getEmployee();
							} else {
								uni.hideLoading()
								uni.showToast({
									title: '安全令牌获取失败',
									icon: 'none'
								})
							}
						}).catch((err) => {
							uni.hideLoading();
							uni.showToast({
								title: `登录失败，错误信息为：${err.message}`,
								icon: 'none'
							});
							console.error(err);
						})
					}
				});
			},
			onGotUserInfo(info) {
				if (!info.detail.iv) {
					uni.showToast({
						title: '授权失败',
						icon: 'none'
					});
					return;
				}
				console.log(info);
				app.globalData.userInfo = info.detail.userInfo;
				app.globalData.encryptedData = info.detail.encryptedData;
				app.globalData.iv = info.detail.iv;
				this.login();
			}
		},
		onLoad() {
			// #ifdef  MP-WEIXIN
			// uni.removeStorageSync('token');
			this.hasLogin = this.checkTokenCache();
			const employee = app.globalData.employee;
			if (this.hasLogin) {
				if (employee.company) {
					this.showBind = false;
					this.btnTxt = '员工健康打卡';
				} else {
					this.getEmployee();
				}
			}
			// #endif
		}
	}
</script>

<style>
	/*  #ifdef  MP-WEIXIN  */
	.wxTipTitle {
		font-size: 20px;
		font-weight: bold;
		border-bottom: 1px solid #E5E5E5;
		text-align: center;
	}

	.wxTipCon {
		font-size: 14px;
		margin: 15px 5%;
		color: #ADADAD;
		text-align: left;
	}

	/*  #endif  */
	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
		margin-bottom: 10px;
	}

	.action-row navigator {
		color: #007aff;
		padding: 0 10px;
	}
</style>
